package com.pachong.android.baseuicomponent.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.pachong.android.baseuicomponent.LoadState;


public class BottomLoadStateView extends LinearLayout {

	private LoadState mState;
	private View mIdleLayout; // 对应 状态 IDLE
	private View mLoadingLayout; // 对应 状态 LOADING
	private View mErrorLayout;// 对应 状态 LOADING_ERROR
	private View mFinishLayout;// 对应 状态 LOADING_FINISH
	private View mUnreachNetLayout;// 对应 状态 LOADING_UNREACH_NETWORK


	public BottomLoadStateView(Context context, View idleLayout, View loadingLayout, View errorLayout,
							   View finishLayout, View unreachNetLayout) {
		super(context);
		setGravity(Gravity.CENTER);
		RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
				RecyclerView.LayoutParams.WRAP_CONTENT);
		setLayoutParams(lp);
		mIdleLayout = idleLayout;
		mLoadingLayout = loadingLayout;
		mErrorLayout = errorLayout;
		mFinishLayout = finishLayout;
		mUnreachNetLayout = unreachNetLayout;
		setState(LoadState.IDLE);
	}


	public BottomLoadStateView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}


	public void setState(LoadState state) {
		if (state != mState) {
			mState = state;
			View currLayout = getLayoutBy(mState);
			this.removeAllViews();
			this.addView(currLayout);

		}
	}
	
	
	public LoadState getState() {
		return mState;
	}


	private View getLayoutBy(LoadState state) {
		switch (state) {
		case IDLE:
			return mIdleLayout;
		case LOADING:
			return mLoadingLayout;
		case ERROR:
			return mErrorLayout;
		case FINISH:
			return mFinishLayout;
		case INVALID_NETWORK:
			return mUnreachNetLayout;
		default:
			return mIdleLayout;
		}
	}


}
